from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin,ListModelMixin,UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin
from rest_framework.response import Response

from book.models import BookInfo
from book.serializers import BookSerializer


class BooksView(GenericAPIView,CreateModelMixin,ListModelMixin):
    """
        获取所有图书和保存图书
    """
    # 指定序列化器属性
    serializer_class = BookSerializer
    # 指定查询集属性
    queryset = BookInfo.objects.all()

    def get(self, request):
      return self.list(request)

    def post(self, request):
        return self.create(request)



class BookView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    """
        获取单一图书
        更新
        删除
    """
    # 指定序列化器属性
    serializer_class = BookSerializer
    # 指定查询集属性
    queryset = BookInfo.objects.all()

    def get(self, request, pk):
        return self.retrieve(request)

    def put(self, request, pk):
        return self.update(request)

    def delete(self, request, pk):
        return self.destroy(request)